package org.wuqi.microweb.api.exception;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wuqi.microweb.api.result.BaseInfoRrrorResult;

import java.util.Set;

/**
 * @author jjj
 * @program: earnest-money
 * @description:TODO
 * @date 2023-12-29 14:19:37
 *
 */
//使用自定义全局注解
@ControllerAdvice
public class MyException {
    @ResponseBody
    @ExceptionHandler(value = ConstraintViolationException.class)
    public BaseInfoRrrorResult exceptionHandle(ConstraintViolationException e){
        System.out.println("====="+e.getMessage());
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        System.out.println(constraintViolations);
        StringBuilder stringBuilder = new StringBuilder();
        constraintViolations.forEach(item-> stringBuilder.append(item.getMessage()+";"));
        BaseInfoRrrorResult baseInfoRrrorResult = new BaseInfoRrrorResult(400,stringBuilder.toString());
        return baseInfoRrrorResult;
    }
}
 
 